<template>
  <div class="medicalRecord-container">
    <!-- 就诊历史 -->
    <div class="medicalRecord-list-container">
      <div class="medicalRecord-title-container">
        <div class="title"><p class="title-con">就诊历史</p></div>
      </div>
      <div class="list-container">
        <template v-if="caseHistoryLists && caseHistoryLists.length">
          <el-table
            :data="caseHistoryLists"
            :show-header="false"
            :highlight-current-row="true"
            :row-key="getRowKeys"
            :expand-row-keys="expands"
            @current-change="toggleRowExpansion"
            @expand-change="expandSelect"
            width="100%"
            style="width: 100%"
          >
            <el-table-column type="expand">
              <template slot-scope="props">
                <div class="item-container" v-if="prescriptionList.length>0||checkItems.length>0">
                  <!-- 最近处方 -->
                  <div class="prescription-container" v-if="prescriptionList.length">
                    <div class="prescription-title-container">
                      <div class="title"><p class="title-con">最近处方</p></div>
                    </div>
                    <div class="list-container prescription-list-container">
                      <div
                        class="prescription-item-container"
                      >
                        <div
                          class="prescription-container"
                          v-for="(pers, pridx) in prescriptionList"
                          :key="pridx"
                        >
                          <div class="list-title">
                            <p>
                              <i
                                class="list-icon el-icon-files"
                                v-if="pers.category_key == 'cycf'"
                              ></i>
                              <i
                                class="list-icon el-icon-notebook-1"
                                v-else-if="pers.category_key == 'ypcf'"
                              ></i>
                              <i
                                class="list-icon el-icon-notebook-2"
                                v-else-if="pers.category_key == 'sycf'"
                              ></i>
                              <span>{{ pers.category_name }}</span>
                            </p>
                          </div>
                          <ul class="list-ul">
                            <li
                              class="list-item"
                              v-for="(pro, ix) in pers.details"
                              :key="ix"
                            >
                              <div class="item-product">
                                <span class="content">{{
                                  pro.product_name
                                }}</span>
                              </div>
                              <div class="item-unit">
                                <span>{{ pro.number }}</span>
                                <span>{{ pro.unit }}</span>
                              </div>
                            </li>
                          </ul>
                        </div>
                      </div>
                    </div>
                  </div>
                  <!-- 检查治疗项 -->
                  <div class="checkItem-container" v-if="checkItems.length">
                    <div class="checkItem-title-container">
                      <div class="title">
                        <p class="title-con">检查治疗项</p>
                      </div>
                    </div>
                    <div class="list-container check-item-container">
                      <ul
                        class="list-ul"
                      >
                        <li
                          class="list-item"
                          v-for="(check, idx) in checkItems"
                          :key="idx"
                        >
                          <div class="item-name">
                            <i class="list-icon el-icon-bank-card"></i>
                            <span class="content">{{
                              check.check_item_name
                            }}</span>
                          </div>
                          <div class="item-unit">
                            <span class="content">{{
                              check.check_item_number
                            }}</span>
                            <span class="content">次</span>
                          </div>
                        </li>
                      </ul>
                    </div>
                  </div>
                </div>
                <div class="data-null" v-else>
                  <blank picWidth="120px" title="暂无处方和检查项"></blank>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="商品 ID" width="90px">
              <template slot-scope="scope">
                <p class="time" style="white-space: nowrap;">{{scope.row.created_at | dateFormat("yyyy-mm-dd")}}</p>
              </template>
            </el-table-column>
            <el-table-column label="商品名称" prop="name">
              <template slot-scope="scope">
                <p class="text-line-1">{{ scope.row.result }}</p>
              </template>
            </el-table-column>
          </el-table>
        </template>
        <div class="data-null" v-else>
          <blank picWidth="120px"></blank>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { historyListDetail, caseHistoryList } from "@/api/outpatient.js";
import blank from "../../../components/blank.vue";
export default {
  components: { blank },
  props: {
    resumeId: {
      type: Number,
      default: () => {},
    },
  },
  data() {
    return {
      caseHistoryLists: [],
      historyListPost: {
        resume_id: 0,
        pager: {
          number: 1,
          size: 5,
        },
      },
      selectId: 0,
      checkItems: [],
      prescriptionList: [],
      expands: [],
      // 获取row的key值
      getRowKeys(row) {
        return row.ID;
      },
    };
  },
  watch: {
    selectId: {
      handler(newval, oldval) {
        if (newval > 0) {
          this.getHistoryListDetail(newval);
        }
      },
    },
    resumeId: {
      handler(newval, oldval) {
        if (newval > 0) {
          this.expands = [];
          this.historyListPost.resume_id = newval;
          this.getCaseHistoryList(this.historyListPost);
        } else {
          this.selectId = 0;
          this.caseHistoryLists = [];
          this.checkItems = [];
          this.prescriptionList = [];
        }
      },
    },
  },
  created() {},
  methods: {
    hisListClick(id) {
      this.selectId = id;
    },
    toggleRowExpansion(row) {
      this.expands = [];
      this.expands.push(row.ID);
      this.selectId = row.ID;
    },
    // 折叠面板每次只能展开一行
    expandSelect(row, expandedRows) {
      // console.log('expandedRows', expandedRows)
      // console.log('row', row)
      var that = this
      if (expandedRows.length) {
        that.expands = []
        if (row) {
          that.expands.push(row.ID)// 每次push进去的是每行的ID
          that.selectId = row.ID
        }
      } else {
        that.expands = []// 默认不展开
      }
      // console.log('that.expands', that.expands)
    },
    getCaseHistoryList(data) {
      var that = this;
      caseHistoryList(data).then((res) => {
        console.log(res.data.data || []);
        that.caseHistoryLists = res.data ? res.data.data : [];
        if (that.caseHistoryLists.length > 0) {
          that.selectId = that.caseHistoryLists[0].ID;
          that.expands.push(that.selectId)
        }
      });
    },
    getHistoryListDetail(id) {
      var that = this;
      historyListDetail(id).then((res) => {
        if (res.code == 200) {
          let datas = res.data;
          if (datas) {
            that.checkItems = datas.check_items || [];
            that.prescriptionList = datas.prescriptions||[];
          }
        }
      });
    },
  },
};
</script>

<style lang="scss">
@import "@/assets/scss/outpatient/modules/medicalRecord.scss";
</style>